<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 数据结构</h1>
        <p>本章节我们主要结合前面所学的知识点来介绍Python数据结构。</p>

        <h2>列表</h2>
        <p>Python中列表是可变的，这是它区别于字符串和元组的最重要的特点，一句话概括即：列表可以修改，而字符串和元组不能。
        </p>
        <p>以下是 Python 中列表的方法：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th>方法</th>
                <th>描述</th>
            </tr>
            <tr>
                <td>list.append(x)
                </td>
                <td>把一个元素添加到列表的结尾，相当于 a[len(a):] = [x]。
                </td>
            </tr>

            <tr>
                <td>list.extend(L)
                </td>
                <td>通过添加指定列表的所有元素来扩充列表，相当于 a[len(a):] = L。
                </td>
            </tr>

            <tr>
                <td>list.insert(i, x)
                </td>
                <td>在指定位置插入一个元素。第一个参数是准备插入到其前面的那个元素的索引，例如 a.insert(0, x) 会插入到整个列表之前，而 a.insert(len(a), x) 相当于
                    a.append(x) 。
                </td>
            </tr>
            <tr>
                <td>list.remove(x)
                </td>
                <td>删除列表中值为 x 的第一个元素。如果没有这样的元素，就会返回一个错误。
                </td>
            </tr>

            <tr>
                <td>list.pop([i])
                </td>
                <td>从列表的指定位置删除元素，并将其返回。如果没有指定索引，a.pop()返回最后一个元素。元素随即从列表中被删除。（方法中 i 两边的方括号表示这个参数是可选的，而不是要求你输入一对方括号，你会经常在
                    Python 库参考手册中遇到这样的标记。）
                </td>
            </tr>

            <tr>
                <td>list.clear()
                </td>
                <td>移除列表中的所有项，等于del a[:]。
                </td>
            </tr>

            <tr>
                <td>list.index(x)
                </td>
                <td>返回列表中第一个值为 x 的元素的索引。如果没有匹配的元素就会返回一个错误。
                </td>
            </tr>

            <tr>
                <td>list.count(x)
                </td>
                <td>返回 x 在列表中出现的次数。
                </td>
            </tr>

            <tr>
                <td>list.sort()
                </td>
                <td>对列表中的元素进行排序。
                </td>
            </tr>

            <tr>
                <td>list.reverse()
                </td>
                <td>倒排列表中的元素。
                </td>
            </tr>

            <tr>
                <td>list.copy()
                </td>
                <td>返回列表的浅复制，等于a[:]。
                </td>
            </tr>
            </tbody>
        </table>
        <p>下面示例演示了列表的大部分方法：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[</span><span class="lit">66.25</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">333</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">333</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1234.5</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">a</span><span class="pun">.</span><span
                class="pln">count</span><span class="pun">(</span><span class="lit">333</span><span
                class="pun">),</span><span class="pln"> a</span><span class="pun">.</span><span class="pln">count</span><span
                class="pun">(</span><span class="lit">66.25</span><span class="pun">),</span><span class="pln"> a</span><span
                class="pun">.</span><span class="pln">count</span><span class="pun">(</span><span class="str">'x'</span><span
                class="pun">))</span><span class="pln">
</span><span class="lit">2</span><span class="pln"> </span><span class="lit">1</span><span class="pln"> </span><span
                class="lit">0</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a</span><span class="pun">.</span><span class="pln">insert</span><span
                class="pun">(</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">-</span><span class="lit">1</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a</span><span class="pun">.</span><span class="pln">append</span><span
                class="pun">(</span><span class="lit">333</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a
</span><span class="pun">[</span><span class="lit">66.25</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">333</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">-</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">333</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1234.5</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">333</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a</span><span class="pun">.</span><span
                class="pln">index</span><span class="pun">(</span><span class="lit">333</span><span class="pun">)</span><span
                class="pln">
</span><span class="lit">1</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a</span><span class="pun">.</span><span class="pln">remove</span><span
                class="pun">(</span><span class="lit">333</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a
</span><span class="pun">[</span><span class="lit">66.25</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">-</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">333</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1234.5</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">333</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a</span><span class="pun">.</span><span class="pln">reverse</span><span
                class="pun">()</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a
</span><span class="pun">[</span><span class="lit">333</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1234.5</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">333</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">-</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">66.25</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a</span><span class="pun">.</span><span
                class="pln">sort</span><span class="pun">()</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a
</span><span class="pun">[-</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">66.25</span><span class="pun">,</span><span class="pln"> </span><span class="lit">333</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">333</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">1234.5</span><span class="pun">]</span></pre>
        <p>注意：类似 insert, remove 或 sort 等修改列表的方法没有返回值。</p>

        <h2>将列表当做堆栈使用</h2>
        <p>
            列表方法使得列表可以很方便的作为一个堆栈来使用，堆栈作为特定的数据结构，最先进入的元素最后一个被释放（后进先出）。用 append() 方法可以把一个元素添加到堆栈顶。用不指定索引的 pop()
            方法可以把一个元素从堆栈顶释放出来。例如：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> stack </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span
                class="lit">3</span><span class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">]</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> stack</span><span class="pun">.</span><span class="pln">append</span><span
                class="pun">(</span><span class="lit">6</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> stack</span><span class="pun">.</span><span class="pln">append</span><span
                class="pun">(</span><span class="lit">7</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> stack
</span><span class="pun">[</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">7</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> stack</span><span class="pun">.</span><span class="pln">pop</span><span
                class="pun">()</span><span class="pln">
</span><span class="lit">7</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> stack
</span><span class="pun">[</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span class="pun">]</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> stack</span><span class="pun">.</span><span class="pln">pop</span><span
                class="pun">()</span><span class="pln">
</span><span class="lit">6</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> stack</span><span class="pun">.</span><span class="pln">pop</span><span
                class="pun">()</span><span class="pln">
</span><span class="lit">5</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> stack
</span><span class="pun">[</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">4</span><span class="pun">]</span></pre>

        <h2>将列表当作队列使用</h2>
        <p>也可以把列表当做队列用，只是在队列里第一加入的元素，第一个取出来；但是拿列表用作这样的目的效率不高。在列表的最后添加或者弹出元素速度快，然而在列表里插入或者从头部弹出速度却不快（因为所有其他的元素都得一个一个地移动）。
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">from</span><span class="pln"> collections </span><span
                class="kwd">import</span><span class="pln"> deque
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> queue </span><span class="pun">=</span><span class="pln"> deque</span><span
                class="pun">([</span><span class="str">"Eric"</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">"John"</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">"Michael"</span><span class="pun">])</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> queue</span><span class="pun">.</span><span class="pln">append</span><span
                class="pun">(</span><span class="str">"Terry"</span><span class="pun">)</span><span
                class="pln">           </span><span class="com"># Terry arrives</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> queue</span><span class="pun">.</span><span class="pln">append</span><span
                class="pun">(</span><span class="str">"Graham"</span><span class="pun">)</span><span
                class="pln">          </span><span class="com"># Graham arrives</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> queue</span><span class="pun">.</span><span class="pln">popleft</span><span
                class="pun">()</span><span class="pln">                 </span><span class="com"># The first to arrive now leaves</span><span
                class="pln">
</span><span class="str">'Eric'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> queue</span><span class="pun">.</span><span class="pln">popleft</span><span
                class="pun">()</span><span class="pln">                 </span><span class="com"># The second to arrive now leaves</span><span
                class="pln">
</span><span class="str">'John'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> queue                           </span><span class="com"># Remaining queue in order of arrival</span><span
                class="pln">
deque</span><span class="pun">([</span><span class="str">'Michael'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'Terry'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Graham'</span><span class="pun">])</span></pre>

        <h2>列表推导式</h2>
        <p>列表推导式提供了从序列创建列表的简单途径。通常应用程序将一些操作应用于某个序列的每个元素，用其获得的结果作为生成新列表的元素，或者根据确定的判定条件创建子序列。
        </p>
        <p>
            每个列表推导式都在 for 之后跟一个表达式，然后有零到多个 for 或 if 子句。返回结果是一个根据表达从其后的 for 和 if 上下文环境中生成出来的列表。如果希望表达式推导出一个元组，就必须使用括号。

        </p>
        <p>这里我们将列表中每个数值乘三，获得一个新的列表：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> vec </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="lit">3</span><span class="pun">*</span><span class="pln">x </span><span
                class="kwd">for</span><span class="pln"> x </span><span class="kwd">in</span><span
                class="pln"> vec</span><span class="pun">]</span><span class="pln">
</span><span class="pun">[</span><span class="lit">6</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">12</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">18</span><span class="pun">]</span></pre>
        <p>现在我们玩一点小花样：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="pun">[[</span><span class="pln">x</span><span class="pun">,</span><span
                class="pln"> x</span><span class="pun">**</span><span class="lit">2</span><span
                class="pun">]</span><span class="pln"> </span><span class="kwd">for</span><span
                class="pln"> x </span><span class="kwd">in</span><span class="pln"> vec</span><span class="pun">]</span><span
                class="pln">
</span><span class="pun">[[</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">4</span><span class="pun">],</span><span class="pln"> </span><span class="pun">[</span><span
                class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span class="lit">16</span><span
                class="pun">],</span><span class="pln"> </span><span class="pun">[</span><span class="lit">6</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">36</span><span
                class="pun">]]</span></pre>
        <p>
            这里我们对序列里每一个元素逐个调用某方法：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> freshfruit </span><span
                class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span
                class="str">'  banana'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'  loganberry '</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'passion fruit  '</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">weapon</span><span class="pun">.</span><span class="pln">strip</span><span
                class="pun">()</span><span class="pln"> </span><span class="kwd">for</span><span
                class="pln"> weapon </span><span class="kwd">in</span><span class="pln"> freshfruit</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">[</span><span class="str">'banana'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'loganberry'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'passion fruit'</span><span class="pun">]</span></pre>
        <p>我们可以用 if 子句作为过滤器： </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="pun">[</span><span class="lit">3</span><span class="pun">*</span><span
                class="pln">x </span><span class="kwd">for</span><span class="pln"> x </span><span class="kwd">in</span><span
                class="pln"> vec </span><span class="kwd">if</span><span class="pln"> x </span><span
                class="pun">&gt;</span><span class="pln"> </span><span class="lit">3</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">[</span><span class="lit">12</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">18</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="lit">3</span><span class="pun">*</span><span class="pln">x </span><span
                class="kwd">for</span><span class="pln"> x </span><span class="kwd">in</span><span
                class="pln"> vec </span><span class="kwd">if</span><span class="pln"> x </span><span
                class="pun">&lt;</span><span class="pln"> </span><span class="lit">2</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">[]</span></pre>
        <p>以下是一些关于循环和其它技巧的演示：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> vec1 </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[</span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> vec2 </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">[</span><span class="lit">4</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">3</span><span class="pun">,</span><span class="pln"> </span><span
                class="pun">-</span><span class="lit">9</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">x</span><span class="pun">*</span><span class="pln">y </span><span
                class="kwd">for</span><span class="pln"> x </span><span class="kwd">in</span><span
                class="pln"> vec1 </span><span class="kwd">for</span><span class="pln"> y </span><span
                class="kwd">in</span><span class="pln"> vec2</span><span class="pun">]</span><span class="pln">
</span><span class="pun">[</span><span class="lit">8</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">6</span><span class="pun">,</span><span class="pln"> </span><span class="pun">-</span><span
                class="lit">18</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">16</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">12</span><span class="pun">,</span><span class="pln"> </span><span class="pun">-</span><span
                class="lit">36</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">24</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">18</span><span class="pun">,</span><span class="pln"> </span><span class="pun">-</span><span
                class="lit">54</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">x</span><span class="pun">+</span><span class="pln">y </span><span
                class="kwd">for</span><span class="pln"> x </span><span class="kwd">in</span><span
                class="pln"> vec1 </span><span class="kwd">for</span><span class="pln"> y </span><span
                class="kwd">in</span><span class="pln"> vec2</span><span class="pun">]</span><span class="pln">
</span><span class="pun">[</span><span class="lit">6</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="pun">-</span><span
                class="lit">7</span><span class="pun">,</span><span class="pln"> </span><span class="lit">8</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">7</span><span class="pun">,</span><span
                class="pln"> </span><span class="pun">-</span><span class="lit">5</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">10</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">9</span><span class="pun">,</span><span class="pln"> </span><span class="pun">-</span><span
                class="lit">3</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="pun">[</span><span
                class="pln">vec1</span><span class="pun">[</span><span class="pln">i</span><span
                class="pun">]*</span><span class="pln">vec2</span><span class="pun">[</span><span
                class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span
                class="kwd">for</span><span class="pln"> i </span><span class="kwd">in</span><span
                class="pln"> range</span><span class="pun">(</span><span class="pln">len</span><span
                class="pun">(</span><span class="pln">vec1</span><span class="pun">))]</span><span class="pln">
</span><span class="pun">[</span><span class="lit">8</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">12</span><span class="pun">,</span><span class="pln"> </span><span class="pun">-</span><span
                class="lit">54</span><span class="pun">]</span></pre>
        <p>列表推导式可以使用复杂表达式或嵌套函数：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="pun">[</span><span class="pln">str</span><span
                class="pun">(</span><span class="pln">round</span><span class="pun">(</span><span class="lit">355</span><span
                class="pun">/</span><span class="lit">113</span><span class="pun">,</span><span
                class="pln"> i</span><span class="pun">))</span><span class="pln"> </span><span
                class="kwd">for</span><span class="pln"> i </span><span class="kwd">in</span><span
                class="pln"> range</span><span class="pun">(</span><span class="lit">1</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span class="pun">)]</span><span
                class="pln">
</span><span class="pun">[</span><span class="str">'3.1'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'3.14'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'3.142'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'3.1416'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'3.14159'</span><span
                class="pun">]</span></pre>

        <h2>嵌套列表解析</h2>
        <p>
            Python的列表还可以嵌套。
        </p>
        <p>以下实例展示了3X4的矩阵列表：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> matrix </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="pun">[</span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">4</span><span class="pun">],</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="pun">[</span><span
                class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">7</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">8</span><span class="pun">],</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="pun">[</span><span
                class="lit">9</span><span class="pun">,</span><span class="pln"> </span><span class="lit">10</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">11</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">12</span><span class="pun">],</span><span class="pln">
</span><span class="pun">...</span><span class="pln"> </span><span class="pun">]</span></pre>
        <p>以下实例将3X4的矩阵列表转换为4X3列表：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="pun">[[</span><span class="pln">row</span><span
                class="pun">[</span><span class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span
                class="kwd">for</span><span class="pln"> row </span><span class="kwd">in</span><span
                class="pln"> matrix</span><span class="pun">]</span><span class="pln"> </span><span
                class="kwd">for</span><span class="pln"> i </span><span class="kwd">in</span><span
                class="pln"> range</span><span class="pun">(</span><span class="lit">4</span><span class="pun">)]</span><span
                class="pln">
</span><span class="pun">[[</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">9</span><span
                class="pun">],</span><span class="pln"> </span><span class="pun">[</span><span class="lit">2</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">10</span><span class="pun">],</span><span
                class="pln"> </span><span class="pun">[</span><span class="lit">3</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">7</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">11</span><span class="pun">],</span><span class="pln"> </span><span
                class="pun">[</span><span class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">8</span><span class="pun">,</span><span class="pln"> </span><span class="lit">12</span><span
                class="pun">]]</span></pre>
        <p>以下实例也可以使用以下方法来实现：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> transposed </span><span
                class="pun">=</span><span class="pln"> </span><span class="pun">[]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">for</span><span
                class="pln"> i </span><span class="kwd">in</span><span class="pln"> range</span><span
                class="pun">(</span><span class="lit">4</span><span class="pun">):</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     transposed</span><span class="pun">.</span><span class="pln">append</span><span
                class="pun">([</span><span class="pln">row</span><span class="pun">[</span><span
                class="pln">i</span><span class="pun">]</span><span class="pln"> </span><span
                class="kwd">for</span><span class="pln"> row </span><span class="kwd">in</span><span
                class="pln"> matrix</span><span class="pun">])</span><span class="pln">
</span><span class="pun">...</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> transposed
</span><span class="pun">[[</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">9</span><span
                class="pun">],</span><span class="pln"> </span><span class="pun">[</span><span class="lit">2</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">10</span><span class="pun">],</span><span
                class="pln"> </span><span class="pun">[</span><span class="lit">3</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">7</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">11</span><span class="pun">],</span><span class="pln"> </span><span
                class="pun">[</span><span class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">8</span><span class="pun">,</span><span class="pln"> </span><span class="lit">12</span><span
                class="pun">]]</span></pre>
        <p>另外一种实现方法：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> transposed </span><span
                class="pun">=</span><span class="pln"> </span><span class="pun">[]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">for</span><span
                class="pln"> i </span><span class="kwd">in</span><span class="pln"> range</span><span
                class="pun">(</span><span class="lit">4</span><span class="pun">):</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="com"># the following 3 lines implement the nested listcomp</span><span
                class="pln">
</span><span class="pun">...</span><span class="pln">     transposed_row </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">[]</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">for</span><span
                class="pln"> row </span><span class="kwd">in</span><span class="pln"> matrix</span><span
                class="pun">:</span><span class="pln">
</span><span class="pun">...</span><span class="pln">         transposed_row</span><span class="pun">.</span><span
                class="pln">append</span><span class="pun">(</span><span class="pln">row</span><span
                class="pun">[</span><span class="pln">i</span><span class="pun">])</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     transposed</span><span class="pun">.</span><span class="pln">append</span><span
                class="pun">(</span><span class="pln">transposed_row</span><span class="pun">)</span><span class="pln">
</span><span class="pun">...</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> transposed
</span><span class="pun">[[</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">5</span><span class="pun">,</span><span class="pln"> </span><span class="lit">9</span><span
                class="pun">],</span><span class="pln"> </span><span class="pun">[</span><span class="lit">2</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">6</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">10</span><span class="pun">],</span><span
                class="pln"> </span><span class="pun">[</span><span class="lit">3</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">7</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">11</span><span class="pun">],</span><span class="pln"> </span><span
                class="pun">[</span><span class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">8</span><span class="pun">,</span><span class="pln"> </span><span class="lit">12</span><span
                class="pun">]]</span></pre>

        <h2>del 语句</h2>
        <p>
            使用 del 语句可以从一个列表中依索引而不是值来删除一个元素。这与使用 pop() 返回一个值不同。可以用 del 语句从列表中删除一个切割，或清空整个列表（我们以前介绍的方法是给该切割赋一个空列表）。例如：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[-</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">66.25</span><span class="pun">,</span><span class="pln"> </span><span class="lit">333</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">333</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">1234.5</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">del</span><span
                class="pln"> a</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]</span><span
                class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a
</span><span class="pun">[</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">66.25</span><span class="pun">,</span><span class="pln"> </span><span class="lit">333</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">333</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">1234.5</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">del</span><span
                class="pln"> a</span><span class="pun">[</span><span class="lit">2</span><span class="pun">:</span><span
                class="lit">4</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a
</span><span class="pun">[</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">66.25</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">1234.5</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">del</span><span
                class="pln"> a</span><span class="pun">[:]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a
</span><span class="pun">[]</span></pre>
        <p>也可以用 del 删除实体变量： </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">del</span><span class="pln"> a</span></pre>

        <h2> 元组和序列 </h2>
        <p>
            元组由若干逗号分隔的值组成，例如：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> t </span><span class="pun">=</span><span class="pln"> </span><span class="lit">12345</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">54321</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'hello!'</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> t</span><span class="pun">[</span><span
                class="lit">0</span><span class="pun">]</span><span class="pln">
</span><span class="lit">12345</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> t
</span><span class="pun">(</span><span class="lit">12345</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">54321</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'hello!'</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span
                class="com"># Tuples may be nested:</span><span class="pln">
</span><span class="pun">...</span><span class="pln"> u </span><span class="pun">=</span><span
                class="pln"> t</span><span class="pun">,</span><span class="pln"> </span><span class="pun">(</span><span
                class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">3</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">5</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> u
</span><span class="pun">((</span><span class="lit">12345</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">54321</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'hello!'</span><span class="pun">),</span><span
                class="pln"> </span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">2</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">3</span><span class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span
                class="pun">))</span></pre>
        <p>如你所见，元组在输出时总是有括号的，以便于正确表达嵌套结构。在输入时可能有或没有括号， 不过括号通常是必须的（如果元组是更大的表达式的一部分）。
        </p>

        <h2>集合</h2>
        <p>集合是一个无序不重复元素的集。基本功能包括关系测试和消除重复元素。</p>
        <p>可以用大括号({})创建集合。注意：如果要创建一个空集合，你必须用 set() 而不是 {} ；后者创建一个空的字典，下一节我们会介绍这个数据结构。</p>
        <p>以下是一个简单的演示：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> basket </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">{</span><span class="str">'apple'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'orange'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'apple'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'pear'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'orange'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'banana'</span><span
                class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">basket</span><span class="pun">)</span><span
                class="pln">                      </span><span class="com"># 删除重复的</span><span class="pln">
</span><span class="pun">{</span><span class="str">'orange'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'banana'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'pear'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'apple'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="str">'orange'</span><span
                class="pln"> </span><span class="kwd">in</span><span class="pln"> basket                 </span><span
                class="com"># 检测成员</span><span class="pln">
</span><span class="kwd">True</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="str">'crabgrass'</span><span
                class="pln"> </span><span class="kwd">in</span><span class="pln"> basket
</span><span class="kwd">False</span><span class="pln">

</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="com"># 以下演示了两个集合的操作</span><span
                class="pln">
</span><span class="pun">...</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a </span><span class="pun">=</span><span
                class="pln"> </span><span class="kwd">set</span><span class="pun">(</span><span class="str">'abracadabra'</span><span
                class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> b </span><span class="pun">=</span><span
                class="pln"> </span><span class="kwd">set</span><span class="pun">(</span><span
                class="str">'alacazam'</span><span class="pun">)</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a                                  </span><span
                class="com"># a 中唯一的字母</span><span class="pln">
</span><span class="pun">{</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'r'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'c'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'d'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a </span><span class="pun">-</span><span
                class="pln"> b                              </span><span class="com"># 在 a 中的字母，但不在 b 中</span><span
                class="pln">
</span><span class="pun">{</span><span class="str">'r'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'d'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a </span><span class="pun">|</span><span
                class="pln"> b                              </span><span class="com"># 在 a 或 b 中的字母</span><span
                class="pln">
</span><span class="pun">{</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'c'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'r'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'d'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'m'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'z'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'l'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a </span><span class="pun">&amp;</span><span class="pln"> b                              </span><span
                class="com"># 在 a 和 b 中都有的字母</span><span class="pln">
</span><span class="pun">{</span><span class="str">'a'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'c'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a </span><span class="pun">^</span><span
                class="pln"> b                              </span><span class="com"># 在 a 或 b 中的字母，但不同时在 a 和 b 中</span><span
                class="pln">
</span><span class="pun">{</span><span class="str">'r'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'d'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'b'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'m'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'z'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'l'</span><span class="pun">}</span></pre>
        <p>集合也支持推导式：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">{</span><span class="pln">x </span><span class="kwd">for</span><span
                class="pln"> x </span><span class="kwd">in</span><span class="pln"> </span><span class="str">'abracadabra'</span><span
                class="pln"> </span><span class="kwd">if</span><span class="pln"> x </span><span
                class="kwd">not</span><span class="pln"> </span><span class="kwd">in</span><span
                class="pln"> </span><span class="str">'abc'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> a
</span><span class="pun">{</span><span class="str">'r'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'d'</span><span class="pun">}</span></pre>

        <h2>字典</h2>
        <p>另一个非常有用的 Python 内建数据类型是字典。</p>
        <p>序列是以连续的整数为索引，与此不同的是，字典以关键字为索引，关键字可以是任意不可变类型，通常用字符串或数值。</p>
        <p>理解字典的最佳方式是把它看做无序的键=&gt;值对集合。在同一个字典之内，关键字必须是互不相同。</p>
        <p>一对大括号创建一个空的字典：{}。</p>
        <p>这是一个字典运用的简单例子： </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> tel </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">{</span><span class="str">'jack'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">4098</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'sape'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">4139</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> tel</span><span class="pun">[</span><span class="str">'guido'</span><span
                class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">4127</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> tel
</span><span class="pun">{</span><span class="str">'sape'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">4139</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'guido'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">4127</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'jack'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">4098</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> tel</span><span class="pun">[</span><span class="str">'jack'</span><span
                class="pun">]</span><span class="pln">
</span><span class="lit">4098</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">del</span><span
                class="pln"> tel</span><span class="pun">[</span><span class="str">'sape'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> tel</span><span class="pun">[</span><span class="str">'irv'</span><span
                class="pun">]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span
                class="lit">4127</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> tel
</span><span class="pun">{</span><span class="str">'guido'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">4127</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'irv'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">4127</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'jack'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">4098</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> list</span><span class="pun">(</span><span class="pln">tel</span><span
                class="pun">.</span><span class="pln">keys</span><span class="pun">())</span><span class="pln">
</span><span class="pun">[</span><span class="str">'irv'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'guido'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'jack'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> sorted</span><span class="pun">(</span><span class="pln">tel</span><span
                class="pun">.</span><span class="pln">keys</span><span class="pun">())</span><span class="pln">
</span><span class="pun">[</span><span class="str">'guido'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'irv'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'jack'</span><span class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="str">'guido'</span><span
                class="pln"> </span><span class="kwd">in</span><span class="pln"> tel
</span><span class="kwd">True</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="str">'jack'</span><span
                class="pln"> </span><span class="kwd">not</span><span class="pln"> </span><span
                class="kwd">in</span><span class="pln"> tel
</span><span class="kwd">False</span></pre>
        <p>构造函数 dict() 直接从键值对元组列表中构建字典。如果有固定的模式，列表推导式指定特定的键值对：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> dict</span><span class="pun">([(</span><span class="str">'sape'</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">4139</span><span
                class="pun">),</span><span class="pln"> </span><span class="pun">(</span><span
                class="str">'guido'</span><span class="pun">,</span><span class="pln"> </span><span
                class="lit">4127</span><span class="pun">),</span><span class="pln"> </span><span
                class="pun">(</span><span class="str">'jack'</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">4098</span><span class="pun">)])</span><span class="pln">
</span><span class="pun">{</span><span class="str">'sape'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">4139</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'jack'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">4098</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'guido'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">4127</span><span class="pun">}</span></pre>
        <p>此外，字典推导可以用来创建任意键和值的表达式词典：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="pun">{</span><span class="pln">x</span><span class="pun">:</span><span
                class="pln"> x</span><span class="pun">**</span><span class="lit">2</span><span
                class="pln"> </span><span class="kwd">for</span><span class="pln"> x </span><span
                class="kwd">in</span><span class="pln"> </span><span class="pun">(</span><span class="lit">2</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">6</span><span class="pun">)}</span><span class="pln">
</span><span class="pun">{</span><span class="lit">2</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">4</span><span class="pun">,</span><span class="pln"> </span><span class="lit">4</span><span
                class="pun">:</span><span class="pln"> </span><span class="lit">16</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">6</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">36</span><span class="pun">}</span></pre>
        <p>如果关键字只是简单的字符串，使用关键字参数指定键值对有时候更方便： </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> dict</span><span class="pun">(</span><span class="pln">sape</span><span
                class="pun">=</span><span class="lit">4139</span><span class="pun">,</span><span
                class="pln"> guido</span><span class="pun">=</span><span class="lit">4127</span><span
                class="pun">,</span><span class="pln"> jack</span><span class="pun">=</span><span
                class="lit">4098</span><span class="pun">)</span><span class="pln">
</span><span class="pun">{</span><span class="str">'sape'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">4139</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'jack'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">4098</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'guido'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">4127</span><span class="pun">}</span></pre>

        <h2>遍历技巧</h2>
        <p>
            在字典中遍历时，关键字和对应的值可以使用 items() 方法同时解读出来：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> knights </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">{</span><span class="str">'gallahad'</span><span class="pun">:</span><span
                class="pln"> </span><span class="str">'the pure'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'robin'</span><span class="pun">:</span><span class="pln"> </span><span
                class="str">'the brave'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">for</span><span
                class="pln"> k</span><span class="pun">,</span><span class="pln"> v </span><span
                class="kwd">in</span><span class="pln"> knights</span><span class="pun">.</span><span
                class="pln">items</span><span class="pun">():</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">k</span><span class="pun">,</span><span class="pln"> v</span><span
                class="pun">)</span><span class="pln">
</span><span class="pun">...</span><span class="pln">
gallahad the pure
robin the brave</span></pre>
        <p>在序列中遍历时，索引位置和对应值可以使用 enumerate() 函数同时得到：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">for</span><span class="pln"> i</span><span
                class="pun">,</span><span class="pln"> v </span><span class="kwd">in</span><span
                class="pln"> enumerate</span><span class="pun">([</span><span class="str">'tic'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'tac'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'toe'</span><span class="pun">]):</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">i</span><span class="pun">,</span><span class="pln"> v</span><span
                class="pun">)</span><span class="pln">
</span><span class="pun">...</span><span class="pln">
</span><span class="lit">0</span><span class="pln"> tic
</span><span class="lit">1</span><span class="pln"> tac
</span><span class="lit">2</span><span class="pln"> toe</span></pre>
        <p>同时遍历两个或更多的序列，可以使用 zip() 组合： </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span class="pln"> questions </span><span
                class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span
                class="str">'name'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'quest'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'favorite color'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> answers </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">[</span><span class="str">'lancelot'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'the holy grail'</span><span
                class="pun">,</span><span class="pln"> </span><span class="str">'blue'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">for</span><span
                class="pln"> q</span><span class="pun">,</span><span class="pln"> a </span><span
                class="kwd">in</span><span class="pln"> zip</span><span class="pun">(</span><span
                class="pln">questions</span><span class="pun">,</span><span class="pln"> answers</span><span
                class="pun">):</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">print</span><span
                class="pun">(</span><span class="str">'What is your {0}?  It is {1}.'</span><span
                class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span
                class="pln">q</span><span class="pun">,</span><span class="pln"> a</span><span
                class="pun">))</span><span class="pln">
</span><span class="pun">...</span><span class="pln">
</span><span class="typ">What</span><span class="pln"> </span><span class="kwd">is</span><span
                class="pln"> your name</span><span class="pun">?</span><span class="pln">  </span><span
                class="typ">It</span><span class="pln"> </span><span class="kwd">is</span><span
                class="pln"> lancelot</span><span class="pun">.</span><span class="pln">
</span><span class="typ">What</span><span class="pln"> </span><span class="kwd">is</span><span
                class="pln"> your quest</span><span class="pun">?</span><span class="pln">  </span><span
                class="typ">It</span><span class="pln"> </span><span class="kwd">is</span><span class="pln"> the holy grail</span><span
                class="pun">.</span><span class="pln">
</span><span class="typ">What</span><span class="pln"> </span><span class="kwd">is</span><span class="pln"> your favorite color</span><span
                class="pun">?</span><span class="pln">  </span><span class="typ">It</span><span
                class="pln"> </span><span class="kwd">is</span><span class="pln"> blue</span><span class="pun">.</span></pre>
        <p>要反向遍历一个序列，首先指定这个序列，然后调用 reversed() 函数： </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> </span><span class="kwd">for</span><span class="pln"> i </span><span
                class="kwd">in</span><span class="pln"> reversed</span><span class="pun">(</span><span
                class="pln">range</span><span class="pun">(</span><span class="lit">1</span><span
                class="pun">,</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">2</span><span class="pun">)):</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">i</span><span class="pun">)</span><span class="pln">
</span><span class="pun">...</span><span class="pln">
</span><span class="lit">9</span><span class="pln">
</span><span class="lit">7</span><span class="pln">
</span><span class="lit">5</span><span class="pln">
</span><span class="lit">3</span><span class="pln">
</span><span class="lit">1</span></pre>
        <p>要按顺序遍历一个序列，使用 sorted() 函数返回一个已排序的序列，并不修改原值： </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
                class="pln"> basket </span><span class="pun">=</span><span class="pln"> </span><span
                class="pun">[</span><span class="str">'apple'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'orange'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'apple'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'pear'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'orange'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'banana'</span><span
                class="pun">]</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> </span><span class="kwd">for</span><span
                class="pln"> f </span><span class="kwd">in</span><span class="pln"> sorted</span><span
                class="pun">(</span><span class="kwd">set</span><span class="pun">(</span><span
                class="pln">basket</span><span class="pun">)):</span><span class="pln">
</span><span class="pun">...</span><span class="pln">     </span><span class="kwd">print</span><span
                class="pun">(</span><span class="pln">f</span><span class="pun">)</span><span class="pln">
</span><span class="pun">...</span><span class="pln">
apple
banana
orange
pear</span></pre>

        <h2>参阅文档</h2>
        <ul>
            <li><a target="_blank" href="/python3/python3-list.html">Python3 列表</a></li>
            <li><a target="_blank" href="/python3/python3-tuple.html">Python3 元组</a></li>
            <li><a target="_blank" href="/python3/python3-dictionary.html">Python3 字典</a></li>
        </ul>

    </div>
</div>
</body>
</html>
